<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px">
        <el-link type="primary" style="margin-bottom: 15px;" @click="downloadTemplate">下载导入模版</el-link>
        <sc-upload-file v-model="form.file" :limit="1" tip="最多上传1个文件,单个文件不要超过10M,请上传.xls/.xlsx格式文件">
            <el-button type="primary" size="small" icon="el-icon-upload">上传附件</el-button>
        </sc-upload-file>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				goods: '批量导入产品',
				bom: '批量导入BOM',
				contract: '导入销售订单',
                otherin: '导入其他入库单'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                file:"",
			},
            base:"",
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
                    var res = [];
                    if(this.mode == 'goods'){
                        res = await this.$API.goods.goods.import.post(this.form);
                    }else if(this.mode == 'bom'){
                        res = await this.$API.goods.bom.import.post(this.form);
                    }else if(this.mode == 'contract') {
                        res = await this.$API.contract.contract.import.post(this.form);
                    }else {
                        res = await this.$API.ware.other.import.post(this.form);
                    }
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        setData(data){
            this.base = data
        },
        downloadTemplate(){
            const fileUrl = this.base;
            const link = document.createElement("a");
            link.href = fileUrl;
            link.download = fileUrl;
            link.click();
        },
	}
}
</script>

<style>
    .btn-box{
        color: #409eff;
        padding-bottom: 20px;
        cursor: pointer;
    }
</style>
